//
// Created by liz on 24-11-9.
//

#include "my_queue.h"

#include <bsp_log.h>
#include <stdlib.h>

#include "stddef.h"

myQueue *myQueue_init(uint16_t size) {
    if (size > MAX_QUEUE_SIZE) {
        while(1) {
            //队列大小超限
            LOGERROR("[datastruct] Queue overload!");
        }
    }
    myQueue *queue = (myQueue *)malloc(sizeof(myQueue));
    queue->front = 0;
    queue->rear = 0;
    queue->size = size;
    return queue;
}

bool myQueue_isEmpty(const myQueue *queue) {
    return queue->front == queue->rear;
}

bool myQueue_isFull(const myQueue *queue) {
    return ((queue->rear + 1) % queue->size) == queue->front;
}

int myQueue_push_back(myQueue *queue,void *data) {
    if (myQueue_isFull(queue)) {
        //队列满，无法入队
        return -1;
    }
    queue->items[queue->rear] = data;
    queue->rear = (queue->rear + 1) % queue->size;
    return 0;
}

void *myQueue_pop_front(myQueue *queue) {
    if (myQueue_isEmpty(queue)) {
        return NULL;
    }
    void * dequeued = queue->items[queue->front];
    queue->front = (queue->front + 1) % queue->size;
    return dequeued;
}

// 查看队首元素（不移除它）
void *myQueue_peek(myQueue *queue) {
    if (myQueue_isEmpty(queue)) {
        //printf("队列为空，没有队首元素\n");
        return NULL;
    }
    return queue->items[queue->front];  // 返回队首元素
}


